<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加用户</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            margin: 20px auto;
            overflow: hidden;
            background: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        header {
            background: #333;
            color: #fff;
            padding: 20px;
            text-align: center;
        }

        nav {
            display: flex;
            justify-content: center;
            background: #555;
            padding: 10px 0;
        }

        nav a {
            color: white;
            text-decoration: none;
            padding: 0 15px;
        }

        nav a:hover {
            background: #444;
        }

        form {
            margin-top: 20px;
        }

        form label {
            display: block;
            margin-bottom: 5px;
        }

        form input[type="text"],
        form input[type="email"],
        form input[type="password"],
        form select {
            width: calc(100% - 20px);
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
        }

        form input[type="button"] {
            padding: 10px 20px;
            background: #333;
            color: #fff;
            border: none;
            cursor: pointer;
            float: right;
        }

        form input[type="button"]:hover {
            background: #444;
        }

        footer {
            background: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: relative;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>

<body>
    <header>
        <h1>添加用户</h1>
    </header>
    <nav>
        <a href="index.html">返回首页</a>
        <a href="users.html">用户管理</a>
    </nav>
    <div class="container">
        <h2>添加新用户</h2>
        <form>
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username" required>

            <label for="email">邮箱：</label>
            <input type="email" id="email" name="email" required>

            <label for="password">密码：</label>
            <input type="password" id="password" name="password" required>

            <label for="confirm_password">确认密码：</label>
            <input type="password" id="confirm_password" name="confirm_password" required>

            <label for="role">角色：</label>
            <select id="role" name="role">
                <option value="admin">管理员</option>
                <option value="user">用户</option>
            </select>

            <input type="button" class="adduser" value="添加用户">
        </form>
    </div>
    <footer>
        <p>版权所有 © 2024 新闻网站</p>
    </footer>
</body>
<script src="./js/jquery-3.7.1.min.js"></script>
<script>
    let adduser = document.querySelector(".adduser")
    adduser.onclick = function () {
        let username = document.querySelector("#username")
        let email = document.querySelector("#email")
        let password = document.querySelector("#password")
        let confirm_password = document.querySelector("#confirm_password")
        let role = document.querySelector("#role")
        let cookie = cookieToJson(document.cookie)
        let loginusername = cookie.username
        let loginrole = cookie.role
        $.ajax({
            url: "http://www.dlrb.com:8082/addUser.php",
            type: "POST",
            xhrFields: {
                withCredentials: true
            },
            data: {
                username: username.value,
                mail: email.value,
                password: password.value,
                passwordtoo: confirm_password.value,
                role: role.value,
                loginusername: loginusername,
                loginrole: loginrole,
                credentials: 'include'
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    alert(resp.msg)
                    location = "users.html"
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }
    function cookieToJson(cookieString) {
        // 处理空字符串
        if (!cookieString || typeof cookieString !== 'string') {
            return {};
        }
        // 分割 Cookie 字符串
        const cookies = cookieString.split(';');
        const result = {};
        // 遍历每个 Cookie
        for (const cookie of cookies) {
            // 跳过空字符串
            if (!cookie.trim()) {
                continue;
            }
            // 查找第一个等号位置
            const equalsIndex = cookie.indexOf('=');

            // 如果没有等号，跳过这个 Cookie
            if (equalsIndex === -1) {
                continue;
            }
            // 提取键和值
            const name = cookie.substring(0, equalsIndex).trim();
            let value = cookie.substring(equalsIndex + 1).trim();
            // 处理可能的 URL 编码
            try {
                value = decodeURIComponent(value);
            } catch (e) {
                // 如果解码失败，使用原始值
                console.warn(`Failed to decode cookie value: ${value}`, e);
            }
            // 添加到结果对象
            result[name] = value;
        }
        return result;
    }
</script>

</html>